package com.ATM;

import java.util.Scanner;

public class ATM {


    //登录
    public boolean login(User user, Scanner scanner) {
        for (int i = 0; true; i++) {
            System.out.println("请输入账号：");
            String User = scanner.next();
            System.out.println("请输入密码：");
            String Password = scanner.next();

            if (user.getUserID().equals(User) && user.getPassWord().equals(Password)) {
                System.out.println("登录成功！");
                return true;
            } else {
                {
                    if (i == 2) {
                        System.out.println("三次输入错误，账号冻结！");
                        exit();
                    } else {
                        System.out.println("输入错误，请重新输入！你还剩" + (2-i) + "次机会");
                    }
                }
            }
        }
    }

    //退出
    public void exit() {
        System.exit(0);
    }

    //显示余额
    public void show(User user) {
        System.out.println("您的余额为：" + user.getMoney());
    }

    //取款
    public void withdrawal(User user, Scanner scanner) {
        System.out.println("请输入你所需要取的金额：");
        int money = scanner.nextInt();
        if (user.getMoney() >= money) {
            user.setMoney(user.getMoney() - money);
            System.out.println("成功取款" + money + "元！您的账户剩余" + user.getMoney());
        } else {
            System.out.println("余额不足，请重新输入！如果需要返回请输入0000");
            String back = scanner.next();
            if (back.equals("0000")){
                show(user);
            }else{
            withdrawal(user, scanner);
            }

        }

    }

    //存款
    public void deposit(User user, Scanner scanner) {
        System.out.println("请输入你所需要存的金额：");
        int money = scanner.nextInt();
        user.setMoney(user.getMoney() + money);
        System.out.println("成功存款" + money + "元！您的账户剩余" + user.getMoney());
    }

    //修改密码
    public void changePassword(User user, Scanner scanner) {
        System.out.println("请输入原密码：");
        String Password = scanner.next();
        if (Password.equals(user.getPassWord())) {
            System.out.println("请输入新密码");
            String PassWord = scanner.next();
            user.setPassWord(PassWord);
        } else {
            System.out.println("密码有误，请重新尝试！");
            changePassword(user, scanner);
        }
    }


}
